/*##############################################
#                                               #
#	  MultiPlayer Notepad 0.02 - Client         #
#    Colpaert Pieter - Vansteenlandt Jan        #
#         GPLv3      - (c) 2009                 #
# Docs used:                                    #
#  http://www.linuxhowtos.org/C_C++/socket.htm  #
#                                               #
 ##############################################*/

#include <iostream>
#include <stdio.h>
#include <sys/types.h> 
#include <sys/socket.h> 
#include <netinet/in.h>
#include <netdb.h> 
#include <string>
#include <stdlib.h>

#include "../config.cpp"
using namespace std;

//exceptionhandling C-style
void error(char *msg)
{
    perror(msg);
    exit(1);
}

void menu(){
	//this proc should show the welcome message.
	cout << "Welcome to Multiplayer notepad! This is the client application. So you want to connect to a server?" << endl;
}
int main(){
	menu();
	/*
	############  Q & A  ##########
	*/
	string name;
	cout << "Choose a nickname: ";
	cin >> name;
	string servern;
	cout << "What server do you wish to connect to? ";
	cin >> servern;
	
	/*
	############  Variables we might need  ########
	*/
	int portno= PORT;
	int sockfd, n;
    struct sockaddr_in serv_addr;
    struct hostent *server;
    server = gethostbyname(servern.c_str());
    if (server == NULL) {
        fprintf(stderr,"ERROR, no such host\n");
        exit(0);
    }
    
    //creating a socket (see server linedocs for more information)
    sockfd = socket(AF_INET, SOCK_STREAM, 0);
    bzero((char *) &serv_addr, sizeof(serv_addr));
    serv_addr.sin_family = AF_INET;
    bcopy((char *)server -> h_addr, (char *)&serv_addr.sin_addr.s_addr, server->h_length);
    serv_addr.sin_port = htons(portno);
    //socklen_t is a type based on an int, but than otherwise.
    socklen_t connect3 = sizeof(serv_addr);
    if (connect(sockfd, (struct sockaddr*)&serv_addr, connect3) < 0) error("ERROR connecting");
    char buffer[7];
    int time=0;
    int charsent,chargot;
    while(true){
    	//receiving length of message
    	bzero(buffer, 7);
		chargot = read(sockfd, buffer, 6);
		if (chargot < 0) error("ERROR reading from sssocket");
		int length = atoi(buffer);
		//creating cstring with size of message to receive
		char * message = new char[length+1];
		bzero(message, length+1);
		//receiving message
		chargot = read(sockfd, message, length);
		if (chargot < 0) error("ERROR reading from sssocket");
		//this is the message we received:
    	cout << "ONTVANGEN: " << message << endl; 
    	if(time == 0){
    		//First time: the server has to know me: name, ...
    		//also interpretting the received FIRST message
			string msgtosend;
			msgtosend+= name;
	    	charsent = write(sockfd, msgtosend.c_str(), msgtosend.size()+1);
			if (charsent < 0) error("ERROR writing to socket");
			//just for the record
			time++;
			//don't forget
			delete[] message;
    	}
    	else{
    		//NOT First message
			//
			string dink;
			cin >> dink;
			time ++;
    	}
	}
	return 0;
}

